package com.ling.pl.security.controller;

import com.ling.pl.core.commons.support.DaoPage;
import com.ling.pl.core.commons.utils.PageUtils;
import com.ling.pl.core.commons.utils.StringUtils;
import com.ling.pl.security.model.DefaultUser;
import com.ling.pl.security.model.GroupMember;
import com.ling.pl.security.service.DefaultOrgService;
import com.ling.pl.security.service.DefaultPositionService;
import com.ling.pl.security.service.DefaultUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author bo.wang
 */
@RequestMapping("/defaultGroup")
@Api(value = "defaultGroup", description = "API")
@RestController("defaultGroupController")
public class DefaultGroupController extends BaseDefaultGroupController {

    Log log = LogFactory.getLog(DefaultGroupController.class);

    @Resource
    private DefaultUserService userService;
    @Resource
    private DefaultOrgService orgService;
    @Resource
    private DefaultPositionService positionService;

    @ApiOperation(value = "loadGroupMembers")
    @RequestMapping(value = "/loadGroupMembers", method = RequestMethod.GET)
    public DaoPage loadGroupMembers(Map params) throws Exception {
        DaoPage daoPage = defaultGroupService.loadGroupMembers(params, PageUtils.getPageIndex(params), PageUtils.getPageNumber(params));
        for (GroupMember gm : (List<GroupMember>) daoPage.getResult()) {
            if (StringUtils.isNotEmpty(gm.getUsername())) {
                gm.setUser((DefaultUser) userService.loadUserByUsername(gm.getUsername()));
            }
            if (StringUtils.isNotEmpty(gm.getOrgId())) {
                gm.setOrg(orgService.loadOrgById(gm.getOrgId()));
            }
            if (StringUtils.isNotEmpty(gm.getPositionId())) {
                gm.setPosition(positionService.loadPositionById(gm.getPositionId()));
            }
        }
        return daoPage;
    }

    @ApiOperation(value = "deleteMember")
    @RequestMapping(value = "/deleteMember", method = RequestMethod.PUT)
    public void deleteMember(@ApiParam(value = "memberId") @PathVariable(name = "memberId") String memberId) throws Exception {
        GroupMember gm = new GroupMember();
        gm.setId(memberId);
        defaultGroupService.remove(gm);
    }

    @ApiOperation(value = "insertGroupMembers")
    @RequestMapping(value = "/insertGroupMembers", method = RequestMethod.GET)
    public String insertGroupMembers(@ApiParam(value = "ids") @PathVariable(name = "ids") Collection<String> ids, @ApiParam(value = "groupId") @PathVariable(name = "groupId") String groupId, @ApiParam(value = "type") @PathVariable(name = "type") String type) throws Exception {
        String error = null;
        List<GroupMember> members = new ArrayList<GroupMember>();
        for (String id : ids) {
            GroupMember gm = new GroupMember();
            gm.setId(UUID.randomUUID().toString());
            gm.setGroupId(groupId);
            if (type.equals("user")) {
                Map params = new HashMap();
                params.put("type", type);
                params.put("groupId", groupId);
                params.put("username", id);
                if (defaultGroupService.loadGroupMembers(params).size() > 0) {
                    error = id;
                    break;
                }
                gm.setUsername(id);
            } else if (type.equals("org")) {
                Map params = new HashMap();
                params.put("type", type);
                params.put("groupId", groupId);
                params.put("orgId", id);
                if (defaultGroupService.loadGroupMembers(params).size() > 0) {
                    error = id;
                    break;
                }
                gm.setOrgId(id);
            } else if (type.equals("position")) {
                Map params = new HashMap();
                params.put("type", type);
                params.put("groupId", groupId);
                params.put("positionId", id);
                if (defaultGroupService.loadGroupMembers(params).size() > 0) {
                    error = id;
                    break;
                }
                gm.setPositionId(id);
            }
            members.add(gm);
        }
        if (error == null) {
            for (GroupMember gm : members) {
                defaultGroupService.save(gm);
            }
        }
        return error;
    }
}

